home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Various / DevDisk 65 (1989)(DevWare PD).zip / DevDisk 65 (1989)(DevWare PD).adf / prosuite / strings.c < prev    next >
C/C++ Source or Header  |  1990-07-11  |  4KB  |  208 lines

  1.  
  2. /* *** strings.c ************************************************************
  3.  *
  4.  * String Manipulation Routines
  5.  *
  6.  * Copyright (C) 1986, 1987, Robert J. Mical
  7.  *
  8.  * CONFIDENTIAL and PROPRIETARY
  9.  *
  10.  * HISTORY      NAME            DESCRIPTION
  11.  * -----------  --------------  --------------------------------------------
  12.  * 4 Feb 87     RJ              Real release
  13.  * 2 Feb 87     RJ              Added StripOuterSpace()
  14.  * 16 May 86    RJ              Just more stuff
  15.  * 1 June 1985  =RJ Mical=      Created this file from a weird distant echo.
  16.  *
  17.  * *********************************************************************** */
  18.  
  19.  
  20. #include "exec/types.h"
  21.  
  22. #define FOREVER for(;;)
  23.  
  24.  
  25. /* copy these and define them as 'extern' in your global include file 
  26.  * in order to use these routines in your code.
  27.  */
  28. SHORT CompareUpperStrings();
  29. UBYTE *FindSuffix();
  30. SHORT GetAtom();
  31. SHORT IndexString();
  32. SHORT StringLength();
  33. BOOL StringsEqual();
  34. UBYTE *StripLeadingSpace();
  35. UBYTE *StripOuterSpace();
  36.  
  37.  
  38.  
  39. SHORT CompareUpperStrings(s, t)
  40. UBYTE *s, *t;
  41. /* Compare s to t, making alphabet characters uppercase.  Return value:
  42.  *      s < t         -1
  43.  *      s == t          0
  44.  *      s > t          1
  45.  */
  46. {
  47.     SHORT sc, tc;
  48.  
  49.     FOREVER
  50.         {
  51.         sc = *s++;
  52.         if ((sc >= 'a') && (sc <= 'z')) sc = sc - 'a' + 'A';
  53.         tc = *t++;
  54.         if ((tc >= 'a') && (tc <= 'z')) tc = tc - 'a' + 'A';
  55.  
  56.         if (sc < tc) return(-1);
  57.         if (sc > tc) return(1);
  58.         if (sc == '\0') return(0);
  59.         }
  60.     return(0);
  61. }
  62.  
  63.  
  64.  
  65. VOID CopyString(tostring, fromstring)
  66. UBYTE *tostring, *fromstring;
  67. {
  68.     while (*tostring++ = *fromstring++) ;
  69. }
  70.  
  71.  
  72.  
  73. VOID ConcatString(firststring, addstring)
  74. UBYTE *firststring, *addstring;
  75. {
  76.     SHORT length1;
  77.  
  78.     length1 = StringLength(firststring);
  79.     firststring += length1;
  80.     CopyString(firststring, addstring);
  81. }
  82.  
  83.  
  84.  
  85. UBYTE *FindSuffix(string, suffix)
  86. UBYTE *string;
  87. UBYTE *suffix;
  88. {
  89.     SHORT stringlength, suffixlength;
  90.  
  91.     if ( (stringlength = StringLength(string))
  92.             >= (suffixlength = StringLength(suffix)) )
  93.         {
  94.         if (StringsEqual(string + stringlength - suffixlength, suffix))
  95.             return (string + stringlength - suffixlength);
  96.         }
  97.     return(NULL);
  98. }
  99.  
  100.  
  101.  
  102. SHORT IndexString(lookin, lookfor)
  103. UBYTE *lookin, *lookfor;
  104. /* This routines looks in the lookin string for the lookfor string.
  105.  * Returns the position of the lookfor string in lookin, or
  106.  * returns -1 if the lookfor string was not found.
  107.  */
  108. {
  109.     SHORT index;
  110.     UBYTE *workin, *workfor;
  111.  
  112.     index = 0;
  113.  
  114.     while (*lookin)
  115.         {
  116.         workin = lookin;
  117.         workfor = lookfor;
  118.         while ((*workfor) && (*workin) && (*workfor == *workin))
  119.             {
  120.             workfor++;
  121.             workin++;
  122.             }
  123.         if (*workfor == '\0') return(index);
  124.         lookin++;
  125.         index++;
  126.         }
  127.  
  128.     return(-1);
  129. }
  130.  
  131.  
  132.  
  133. SHORT StringLength(text)
  134. UBYTE *text;
  135. {
  136.     SHORT length;
  137.  
  138.     length = 0;
  139.     while (*text++) length++;
  140.     return(length);
  141. }
  142.  
  143.  
  144.  
  145. BOOL StringsEqual(text1, text2)
  146. UBYTE *text1, *text2;
  147. {
  148.     while (*text1 == *text2)
  149.         {
  150.         if (*text1 == '\0') return(TRUE);
  151.         text1++;
  152.         text2++;
  153.         }
  154.  
  155.     return(FALSE);
  156. }
  157.  
  158.  
  159.  
  160. UBYTE *StripLeadingSpace(text, space)
  161. UBYTE *text, *space;
  162. /* This routine "strips" the leading occurrences of the space string
  163.  * characters from the text string by finding the first character
  164.  * of the text string that is not contained in the space string.
  165.  * The return value is a pointer to the first non-space character in text.
  166.  * If text is the null string or if text consists of nothing
  167.  * but space characters, NULL is returned.
  168.  * If space is the NULL string, text is returned.
  169.  */
  170. {
  171.     if (space == NULL) return(text);
  172.     if (*space == '\0') return(text);
  173.  
  174.     while (*text && (*text == *space)) text++;
  175.  
  176.     if (*text) return(text);
  177.     return(NULL);
  178. }
  179.  
  180.  
  181.  
  182. UBYTE *StripOuterSpace(text, space)
  183. UBYTE *text, *space;
  184. /* This routine "strips" the leading and trailing occurrences of the space 
  185.  * string characters from the text string.
  186.  * The return value is a pointer to the first non-space character in text,
  187.  * and a null byte is stored after the text's last non-space character.
  188.  */
  189. {
  190.     UBYTE *workspace, *lastspace;
  191.  
  192.     if (text == NULL) return(NULL);
  193.  
  194.     if ((workspace = StripLeadingSpace(text, space)) == NULL)
  195.         {
  196.         *text = '\0';
  197.         return(text);
  198.         }
  199.  
  200.     lastspace = workspace + StringLength(workspace) - 1;
  201.     while ((lastspace >= workspace) && (*lastspace == *space)) lastspace--;
  202.     *(lastspace + 1) = '\0';
  203.     return(workspace);
  204. }
  205.  
  206.  
  207.  
  208.